<template>
  <div class="contain-report">
    <div class="report-title">班级管理</div>
    <div class="report-search">
      <el-input
          size="small"
          style="width: 200px"
          placeholder="请输入班级名称"
          clearable
          @clear="getList"
          v-model="form.className"
      >
        <i slot="suffix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="searchList"></i>
      </el-input>
    </div>
    <div class="report-btns">
      <el-button type="primary" v-if="userInfo.roleFlag === 1" size="small" @click="addDialog = true">新建班级
      </el-button>
    </div>
    <div class="report-table">
      <el-table
          :data="tableData"
          style="width: 100%">
        <el-table-column label="班级名称" align="center" prop="className">
        </el-table-column>
        <el-table-column label="学生数量" align="center" prop="num">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" v-if="userInfo.roleFlag === 1" @click="openEditDialog(scope.row)">编 辑</el-button>
            <el-button type="text" @click="openDown(scope.row.id)">查看学生</el-button>
            <el-button type="text" @click="deleteById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="report-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-sizes="[5]"
          :page-size="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>


    <el-drawer
        title="我是标题"
        :visible.sync="drawer"
        @close="getList"
        :with-header="false">
      <div class="drawer-box">
        <div class="main-box">
          <div class="main-input">
            <el-input placeholder="请输入手机号"
                      clearable
                      @clear="getGridList"
                      v-model="gridForm.phone" size="small">
              <i slot="suffix" class="el-input__icon el-icon-search" style="cursor: pointer"
                 @click="searchGridList"></i>
            </el-input>
          </div>
          <div class="main-btn">
            <el-button size="small" @click="downloadFile" style="margin-right: 20px">模板下载</el-button>
            <el-upload
                class="upload-demo"
                :action=uploadUrl
                :headers=myHeaders
                ref="upload"
                :on-success="leadingIn"
                :limit="1"
                :data="uploadData"
                :show-file-list="false"
            >
              <el-button size="small">批量导入</el-button>
            </el-upload>
          </div>
          <div class="main-table">
            <el-table
                :data="gridData"
                style="width: 100%"
                border
            >
              <el-table-column
                  prop="studentNumber"
                  label="学号"
                  align="center">
              </el-table-column>
              <el-table-column
                  prop="phone"
                  label="手机号"
                  align="center">
              </el-table-column>
              <el-table-column
                  prop="userName"
                  label="姓名"
                  align="center">
              </el-table-column>
              <el-table-column
                  label="操作"
                  align="center">
                <template slot-scope="scope">
                  <el-button type="text" @click="getDel(scope.row.id)">移除班级</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="main-page">
            <el-pagination
                background
                layout="prev, pager, next"
                @current-change="handleGridChange"
                :page-size="5"
                :total="gridTotal">
            </el-pagination>
          </div>
        </div>
      </div>
    </el-drawer>
    <el-dialog
        :show-close="false"
        :visible.sync="addDialog"
        width="40%"
    >
      <div class="dialog-box">
        <div class="box-top">新建班级</div>
        <div class="box-body">
          <el-form label-width="80px">
            <el-form-item label="名称:">
              <el-input placeholder="请输入班级名称" v-model="addForm.className"></el-input>
            </el-form-item>
          </el-form>
          <div class="form-btn">
            <el-button type="primary" size="small" @click="submitAdd" style="margin-right: 20px">提 交</el-button>
            <el-button size="small" @click="addDialog = false">取 消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-dialog
        :show-close="false"
        :visible.sync="editDialog"
        width="40%"
    >
      <div class="dialog-box">
        <div class="box-top">编辑班级</div>
        <div class="box-body">
          <el-form label-width="80px">
            <el-form-item label="名称:">
              <el-input placeholder="请输入班级名称" v-model="editForm.className"></el-input>
            </el-form-item>
          </el-form>
          <div class="form-btn">
            <el-button type="primary" size="small" @click="submitEdit" style="margin-right: 20px">提 交</el-button>
            <el-button size="small" @click="editDialog = false">取 消</el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {classPage, deleteClass, classStudent, getOutClass, addClass, editClass} from "../../api/laboratory";
import request from "../../utils/request";
import common from "../../utils/common";

export default {
  name: "laboratory",
  data() {
    return {
      userInfo: null,
      uploadUrl: common.requestUrl + '/upload/upload',
      myHeaders: {
        token: ''
      },
      uploadData: {
        id: null,
        type: 1
      },
      form: {
        className: "",
        pageIndex: 1,
        pageSize: 5
      },
      tableData: [],
      total: null,
      drawer: false,
      gridForm: {
        classId: null,
        pageIndex: 1,
        pageSize: 5,
        phone: ""
      },
      gridTotal: null,
      gridData: [],
      addDialog: false,
      addForm: {
        className: ''
      },
      editDialog: false,
      editForm: {}
    }
  },
  created() {
    this.myHeaders.token = sessionStorage.getItem('token')
    this.userInfo = JSON.parse(sessionStorage.getItem('user'))
  },
  mounted() {
    this.getList()
  },
  methods: {
    downloadFile() {
      request({
        url: common.requestUrl + "/upload/downloadTemplate",
        method: 'get',
        responseType: 'blob',
      }).then(resp => {
        console.log('ppp', resp)
        const blob = new Blob([resp.data], {type: "application/vnd.ms-excel"});
        const url = window.URL.createObjectURL(blob);
        let a = document.createElement('a');
        a.download = '班级模板.xls';
        a.href = url;
        a.click();
      })  //导出文件请求
    },
    leadingIn(res) {
      console.log(res)
      if (res.code !== 200) {
        this.$message.error(res.message)
      } else {
        this.$message.success(res.message)
        this.drawer = false
        this.getList()
      }
      this.$refs.upload.clearFiles() //上传成功之后清除历史记录
    },
    async getList() {
      const {data} = await classPage(this.form)
      console.log(data)
      if (data.code === 200) {
        this.tableData = data.data.data
        this.total = data.data.total
      }
    },
    searchList() {
      if (!this.form.className) {
        this.$message.warning('请输入班级名称')
      } else {
        this.getList()
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.form.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.form.pageIndex = val
      this.getList()
    },
    deleteById(id) {
      this.$confirm('此操作将永久删除该班级, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteClass(id).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.getList()
          }
        })
      })
    },
    getDel(id) {
      this.$confirm('此操作将永久移出该班级, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        getOutClass(id).then(res => {
          console.log(res)
          if (res.data.code === 200) {
            this.$message.success(res.data.message)
            this.getGridList()
          }
        })
      })
    },
    async openDown(id) {
      this.gridForm.phone = ''
      this.gridForm.pageIndex = 1
      this.gridForm.pageSize = 5
      this.gridForm.classId = id
      await this.getGridList()
      this.drawer = true
      this.uploadData.id = id
    },
    async handleGridChange(val) {
      this.gridForm.pageIndex = val
      await this.getGridList()
    },
    async getGridList() {
      const {data} = await classStudent(this.gridForm)
      if (data.code === 200) {
        this.gridData = data.data.data
        this.gridTotal = data.data.total
      }
    },
    searchGridList() {
      if (!this.gridForm.phone) {
        this.$message.error('请输入手机号码')
      } else {
        this.getGridList()
      }
    },
    async submitAdd() {
      if (!this.addForm.className) {
        this.$message.error('请输入班级名称')
      } else {
        const {data} = await addClass(this.addForm)
        if (data.code === 200) {
          this.$message.success(data.message)
          this.addForm = {}
          this.addDialog = false
          await this.getList()
        }
      }
    },
    openEditDialog(item) {
      console.log(item)
      const {className, id} = item
      this.editForm = {className, classId: id}
      this.editDialog = true
    },
    async submitEdit() {
      if (!this.editForm.className) {
        this.$message.error('请输入班级名称')
      } else {
        const {data} = await editClass(this.editForm)
        if (data.code === 200) {
          this.$message.success(data.message)
          this.editForm = {}
          this.editDialog = false
          await this.getList()
        }
      }
    }
  },
}
</script>

<style scoped lang="less">
.contain-report {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;

  .drawer-box {
    width: 100%;
    height: 100%;
    padding: 20px;
    box-sizing: border-box;

    .main-box {
      width: 100%;
      height: 100%;
      padding: 10px;
      box-sizing: border-box;

      .main-input {
        width: 50%;
      }

      .main-btn {
        margin-top: 20px;
        display: flex;
        box-sizing: border-box;
      }

      .main-table {
        margin-top: 20px;
        box-sizing: border-box;
        width: 100%;
      }

      .main-page {
        margin-top: 20px;
        box-sizing: border-box;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }

  .dialog-box {
    width: 100%;
    min-height: 50vh;
    border-radius: 5px;
    overflow: hidden;

    .box-top {
      width: 100%;
      height: 10vh;
      background-image: linear-gradient(to left, #948af1 0%, #c193db 50%, #ed9bc6 100%);
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 26px;
      color: #fff;
    }

    .box-body {
      width: 100%;
      height: 80%;
      padding: 5% 10%;
      box-sizing: border-box;
      background-color: #ffffff;

      /deep/ .el-input__inner {
        border: none;
        background-color: #ecedef;
      }

      /deep/ .el-textarea__inner {
        border: none;
        background-color: #ecedef;
      }

      /deep/ .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }

      /deep/ .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
      }

      /deep/ .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 100px;
        height: 100px;
        line-height: 100px;
        text-align: center;
      }

      /deep/ .avatar {
        width: 100px;
        height: 100px;
        display: block;
      }

      .form-btn {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .file-list {
        margin-top: 20px;
        width: 100%;

        .file-box {
          margin-top: 10px;
          width: 100%;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .left {
            width: 40%;
            display: flex;
            align-items: center;
            justify-content: space-between;
          }
        }
      }
    }
  }

  .report-title {
    font-size: 22px;
    color: #666;
  }

  .report-btns {
    width: 100%;
    display: flex;
    align-items: center;
  }

  .report-search {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;

    p {
      margin: 0 10px;
      box-sizing: border-box;

      &:hover {
        cursor: pointer;
      }
    }
  }

  .report-table {
    width: 100%;
    margin: 20px 0;
    box-sizing: border-box;
  }

  .report-page {
    position: absolute;
    bottom: 10px;
    left: 50%;
    width: 100%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
